package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqXzq;
import cloud.maque.admin.domain.request.MqXzqQueryRequest;
import cloud.maque.admin.domain.response.MqXzqTree;
import cloud.maque.admin.mapper.MqXzqMapper;
import cloud.maque.admin.service.MqXzqService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 4color
 * @description 行政区表
 * @date 2023-03-17
 */
@Service
public class MqXzqServiceImpl extends ServiceImpl<MqXzqMapper, MqXzq> implements MqXzqService {
    @Override
    public MqXzq insert(MqXzq xzq) {
        Assert.hasText(xzq.getXzqName(), "行政区名称不能为空");
        save(xzq);
        return xzq;
    }


    @Autowired
    MqXzqServiceImplHelper helper;

    @Override
    public List<MqXzqTree> xzqTree(String parentcode, String isnew, Boolean getchild, String haveself, Boolean fromredis) {


        String rediskey = helper.GetReidsKey(parentcode, isnew, getchild, haveself);
        List<MqXzqTree> tree = helper.GetTreeRedis(parentcode, isnew, getchild, haveself, fromredis, rediskey);

        if (fromredis) {
            if (tree != null) {
                return tree;
            }
        }

        List<MqXzq> result = list(parentcode, isnew);
        //拷贝
        List<MqXzqTree> xzqTrees = result.stream().map(s -> {
            MqXzqTree t = new MqXzqTree();
            BeanUtils.copyProperties(s, t);

            return t;
        }).collect(Collectors.toList());

        //查询下级
        if (getchild != null && getchild) {
            String topCdoe = !parentcode.equals("0") ? parentcode : "";
            List<MqXzq> allChild = listAllChild(topCdoe, isnew);
            for (MqXzqTree xzq : xzqTrees) {
                helper.FillChild(xzq, allChild);
            }
        }


        if (haveself != null && haveself.equals("1")) {
            xzqTrees = helper.InsertSel(parentcode, xzqTrees);
        }

        if (fromredis) {
            //保存到缓存
            helper.SaveTreeRedis(rediskey, xzqTrees);
        }

        return xzqTrees;
    }


    @Override
    public PageInfo<List<MqXzqTree>> listByPage(PaginateDataResquest<MqXzqQueryRequest> queryRequest) {


        if (queryRequest.getPageIndex() > 0 && queryRequest.getPageSize() > 0) {
            PageHelper.startPage(queryRequest.getPageIndex(), queryRequest.getPageSize());
        }

        QueryWrapper queryWrapper = helper.GetQuery(queryRequest);
        List<MqXzq> list = this.baseMapper.selectList(queryWrapper);

        PageInfo pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public MqXzq DetailById(String xzqId) {
        return this.getById(xzqId);
    }

    @Override
    public MqXzq DetailByCode(String xzqCode) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("xzq_code", xzqCode);
        return this.getOne(queryWrapper, false);

    }

    @Override
    public MqXzq Save(MqXzq xzq) {
        if (StrUtil.isEmptyIfStr(xzq.getXzqId()) || xzq.getXzqId().equals("0")) {
            xzq.setXzqId(UUID.fastUUID().toString());
        }
        Assert.hasText(xzq.getXzqName(), "行政区名称不能为空");
        Assert.hasText(xzq.getXzqCode(), "行政区代码不能为空");

        this.saveOrUpdate(xzq);
        return xzq;
    }

    @Override
    public Boolean Delete(String xzqId) {
        return this.removeById(xzqId);
    }

    @Override
    public Boolean ClearCache() {
        return helper.ClearCache();
    }


    public List<MqXzq> list(String parentcode, String isnew) {

        QueryWrapper<MqXzq> qw = new QueryWrapper();
        qw.lambda().eq(MqXzq::getParentXzqCode, parentcode);
        if (!StrUtil.isEmptyIfStr(isnew) && isnew.equals("1")) {
            qw.lambda().eq(MqXzq::getIsNew, Integer.parseInt(isnew));
        }
        qw.lambda().orderByAsc(MqXzq::getXzqOrder);

        List<MqXzq> result = this.list(qw);
        return result;
    }


    /**
     * 把子子孙孙都列出来
     *
     * @param parentcode
     * @param isnew
     * @return
     */
    public List<MqXzq> listAllChild(String parentcode, String isnew) {

        QueryWrapper<MqXzq> qw = new QueryWrapper();
        qw.lambda().likeRight(MqXzq::getParentXzqCode, parentcode);
        if (!StrUtil.isEmptyIfStr(isnew) && isnew.equals("1")) {
            qw.lambda().eq(MqXzq::getIsNew, Integer.parseInt(isnew));
        }
        qw.lambda().orderByAsc(MqXzq::getXzqOrder);

        List<MqXzq> result = this.list(qw);
        return result;
    }


}
